package com.layui.ad.config.mybatis;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.layui.ad.enums.DataSourceEnum;
import com.layui.ad.multiple.MultipleDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.omg.PortableInterceptor.Interceptor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.context.annotation.Primary;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;

@Configuration
@MapperScan("com.layui.ad.**.mapper")
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        //return new PaginationInterceptor();


        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 开启 PageHelper 的支持
//        paginationInterceptor.setLocalPage(true);
        return paginationInterceptor;
    }


    //*********************以下是新增加的 主要用于调用多个库****************************



    /**
     * SQL执行效率插件 最暴力的方法 把此方法屏蔽后 就不会输出sql
     */
    @Bean
//    @Profile({"dev","qa"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setMaxTime(10000);
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }

    @Bean(name = "broker")
    @ConfigurationProperties(prefix = "spring.datasource.druid.brokerdb" )
    public DataSource broker() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "sys")
    @ConfigurationProperties(prefix = "spring.datasource.druid.sysdb" )
    public DataSource sys() {
        return DruidDataSourceBuilder.create().build();
    }

    /**
     * 动态数据源配置
     * @return
     */
    @Bean
    @Primary
    public DataSource multipleDataSource(@Qualifier("broker") DataSource broker, @Qualifier("sys") DataSource sys) {
        MultipleDataSource multipleDataSource = new MultipleDataSource();
        Map< Object, Object > targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceEnum.BROKER.getValue(), broker);
        targetDataSources.put(DataSourceEnum.SYSSOURCE.getValue(), sys);
        //添加数据源
        multipleDataSource.setTargetDataSources(targetDataSources);
        //设置默认数据源
        multipleDataSource.setDefaultTargetDataSource(sys);
        return multipleDataSource;
    }

}
